package com.lucy.rabc.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.rabc.entity.SysDept;
import com.lucy.rabc.params.DeptCreateParams;
import com.lucy.rabc.params.DeptUpdateParams;
import com.lucy.rabc.service.SysDeptService;
import com.lucy.rabc.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author kindear
 * 部门管理器
 */
@Slf4j
@RestController
@RequestMapping("system")
@Api(tags = "部门管理")
public class DeptController {
    @Autowired
    SysDeptService deptService;

    @Autowired
    SysUserService userService;

    @PostMapping("depts")
    @ApiOperation(value = "部门新建",notes = "")
    @SaCheckPermission(value = "system_dept:add",orRole = "admin")
    public ResponseDTO<SysDept> deptCreate(@RequestBody @Validated DeptCreateParams params){
        /* 初始化构建 **/
        SysDept sysDept = new SysDept();
        BeanUtil.copyProperties(params,sysDept, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        /*
          如果是null 赋值为0
         */
        if(Validator.isEmpty(params.getParentId())){
            params.setParentId(0L);
        }

        return ResponseDTO.succData(deptService.saveOne(sysDept));
    }


    @DeleteMapping("depts/{deptId}")
    @ApiOperation(value = "部门删除",notes = "")
    public ResponseDTO<Object> deptDelete(@PathVariable("deptId") Long deptId){
        deptService.deleteOne(deptId);
        return ResponseDTO.succMsg("删除成功");
    }

    /**
     * 部门信息修改
     */
    @PutMapping("depts")
    @ApiOperation(value = "部门更新",notes = "")
    @SaCheckPermission(value = "system_dept:update",orRole = "admin")
    public ResponseDTO<SysDept> deptUpdate(@RequestBody @Validated DeptUpdateParams params){
        Optional<SysDept> sysDeptOp = deptService.getOne(params.getId());
        if(!sysDeptOp.isPresent())
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        SysDept sysDept = sysDeptOp.get();
        BeanUtil.copyProperties(params,sysDept, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(deptService.updateOne(sysDept));
    }

    @GetMapping("depts/tree")
    @ApiOperation(value = "部门选择树",notes = "")
    public ResponseDTO<Object> buildDeptTree(){
        SysDept dept = new SysDept();
        dept.setParent(null);
        Example<SysDept> example = Example.of(dept);
        List<SysDept> depts = deptService.getExample(example);
        depts = depts.stream().filter(sysDept -> Validator.isEmpty(sysDept.getParent())).collect(Collectors.toList());
        return ResponseDTO.succData(depts);
    }

    @GetMapping("depts/{deptId}")
    @ApiOperation(value = "部门查询",notes = "")
    public ResponseDTO<Object> deptQuery(@PathVariable("deptId") Long deptId){
        Optional<SysDept> deptOp = deptService.getOne(deptId);
        if (!deptOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        return ResponseDTO.succData(
                deptOp.get()
        );
    }

}
